package com.example.mydy;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.mydy.adapter.UserAdvertisementAdapter;
import com.example.mydy.model.Advertisement;
import com.example.mydy.utils.MockDataManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class AdvertisementListActivity extends AppCompatActivity implements 
        UserAdvertisementAdapter.OnAdvertisementClickListener, 
        BottomNavigationView.OnItemSelectedListener {

    private static final String TAG = "AdvertisementListActivity";
    
    private RecyclerView recyclerView;
    private TextView tvNoAdvertisements;
    private ProgressBar progressBar;
    private SwipeRefreshLayout swipeRefreshLayout;
    private BottomNavigationView bottomNavigationView;
    
    private UserAdvertisementAdapter adapter;
    private MockDataManager mockDataManager;
    private boolean isNavigating = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertisement_list);
        
        // 初始化Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar_ad_list);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        
        // 初始化视图
        initViews();
        
        // 获取数据管理器
        mockDataManager = MockDataManager.getInstance();
        
        // 初始化底部导航栏
        setupBottomNavigation();
        
        // 加载广告列表
        loadAdvertisements();
    }
    
    private void initViews() {
        recyclerView = findViewById(R.id.rv_advertisements);
        tvNoAdvertisements = findViewById(R.id.tv_no_advertisements);
        progressBar = findViewById(R.id.progress_bar);
        swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        
        // 设置下拉刷新监听器
        swipeRefreshLayout.setOnRefreshListener(this::loadAdvertisements);
        
        // 设置RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        
        // 初始化适配器
        adapter = new UserAdvertisementAdapter(this, this);
        recyclerView.setAdapter(adapter);
        
        // 添加滚动监听器，支持加载更多
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                
                // 如果滚动到底部，可以在这里添加加载更多数据的逻辑
                if (!recyclerView.canScrollVertically(1) && dy > 0) {
                    // 这里可以添加加载更多数据的逻辑
                    // loadMoreAdvertisements();
                }
            }
        });
    }
    
    private void setupBottomNavigation() {
        // 设置当前选中项为广告
        bottomNavigationView.setSelectedItemId(R.id.nav_advertisement);
        
        // 设置导航菜单项选择监听器
        bottomNavigationView.setOnItemSelectedListener(this);
    }
    
    private void loadAdvertisements() {
        progressBar.setVisibility(View.VISIBLE);
        tvNoAdvertisements.setVisibility(View.GONE);
        
        // 确保在启动时生成一些mock广告数据
        if (mockDataManager.getAllAdvertisements().isEmpty()) {
            generateMockAdvertisements();
        }
        
        // 获取广告列表
        List<Advertisement> advertisements = mockDataManager.getAllAdvertisements();
        
        // 更新UI
        progressBar.setVisibility(View.GONE);
        swipeRefreshLayout.setRefreshing(false);
        
        if (advertisements.isEmpty()) {
            tvNoAdvertisements.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            tvNoAdvertisements.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            adapter.setData(advertisements);
        }
        
        Log.d(TAG, "加载广告数量: " + advertisements.size());
    }
    
    private void generateMockAdvertisements() {
        // 生成一些模拟的广告数据
        List<Advertisement> mockAds = new ArrayList<>();
        
        // 创建5个不同的广告
        Advertisement ad1 = new Advertisement();
        ad1.setId(1);
        ad1.setTitle("限时优惠活动");
        ad1.setContent("参与我们的限时优惠活动，享受高达50%的折扣。活动时间有限，赶快行动吧！");
        ad1.setImageUrl("ad_image_1.jpg");
        ad1.setCreatedAt(new java.util.Date());
        mockAds.add(ad1);
        
        Advertisement ad2 = new Advertisement();
        ad2.setId(2);
        ad2.setTitle("新用户专享福利");
        ad2.setContent("新用户注册即可获得100积分，可兑换多种奖励。邀请好友加入，双方都可获得额外奖励！");
        ad2.setImageUrl("ad_image_2.jpg");
        ad2.setCreatedAt(new java.util.Date());
        mockAds.add(ad2);
        
        Advertisement ad3 = new Advertisement();
        ad3.setId(3);
        ad3.setTitle("夏季产品推荐");
        ad3.setContent("炎炎夏日，为您推荐一系列清凉好物，让您的夏天更加舒适。现在购买还有特别优惠！");
        ad3.setImageUrl("ad_image_3.jpg");
        ad3.setCreatedAt(new java.util.Date());
        mockAds.add(ad3);
        
        Advertisement ad4 = new Advertisement();
        ad4.setId(4);
        ad4.setTitle("企业合作机会");
        ad4.setContent("我们正在寻找优质合作伙伴，共同发展，互利共赢。如果您有兴趣，请立即联系我们的客服团队。");
        ad4.setImageUrl("ad_image_4.jpg");
        ad4.setCreatedAt(new java.util.Date());
        mockAds.add(ad4);
        
        Advertisement ad5 = new Advertisement();
        ad5.setId(5);
        ad5.setTitle("年度会员特惠");
        ad5.setContent("成为我们的年度会员，享受全年不间断的专属优惠和特权服务。现在加入，还可获得额外3个月的会员时间！");
        ad5.setImageUrl("ad_image_5.jpg");
        ad5.setCreatedAt(new java.util.Date());
        mockAds.add(ad5);
        
        // 将这些广告添加到MockDataManager中
        for (Advertisement ad : mockAds) {
            MockDataManager.addAdvertisement(ad);
        }
    }
    
    @Override
    public void onAdvertisementClick(Advertisement advertisement) {
        // 跳转到广告详情页面
        Intent intent = new Intent(this, AdvertisementArticleActivity.class);
        intent.putExtra("ad_id", advertisement.getId());
        startActivity(intent);
    }
    
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if (isNavigating) {
            return false;
        }
        
        isNavigating = true;
        
        int itemId = item.getItemId();
        
        if (itemId == R.id.nav_home) {
            // 先设置选中项为首页，然后再跳转
            bottomNavigationView.setSelectedItemId(R.id.nav_home);
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return true;
        } else if (itemId == R.id.nav_advertisement) {
            // 已经在广告页面，无需操作
            isNavigating = false;
            return true;
        } else if (itemId == R.id.nav_project) {
            // 先设置选中项为项目，然后再跳转
            bottomNavigationView.setSelectedItemId(R.id.nav_project);
            startActivity(new Intent(this, ProjectActivity.class));
            finish();
            return true;
        } else if (itemId == R.id.nav_my) {
            // 先设置选中项为我的，然后再跳转
            bottomNavigationView.setSelectedItemId(R.id.nav_my);
            startActivity(new Intent(this, MyActivity.class));
            finish();
            return true;
        }
        
        isNavigating = false;
        return false;
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 每次回到页面时刷新数据
        loadAdvertisements();
        isNavigating = false;
    }
} 